package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.ConsumeDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TPackConsumeRecordsMapper;
import com.ruoyi.system.domain.TPackConsumeRecords;
import com.ruoyi.system.service.ITPackConsumeRecordsService;

/**
 * 外包消耗记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-12
 */
@Service
public class TPackConsumeRecordsServiceImpl implements ITPackConsumeRecordsService 
{
    @Autowired
    private TPackConsumeRecordsMapper tPackConsumeRecordsMapper;

    @Override
    public ConsumeDataVo allSummary(TPackConsumeRecords tPackConsumeRecords) {
        ConsumeDataVo consumeDataVo = new ConsumeDataVo();
        BigDecimal amount = tPackConsumeRecordsMapper.amountByAll(tPackConsumeRecords);
        consumeDataVo.setAllConsume(amount);
        consumeDataVo.setAllCamExam(tPackConsumeRecordsMapper.profitByAll(tPackConsumeRecords));
        BigDecimal perExam = tPackConsumeRecordsMapper.payByAll(tPackConsumeRecords);
        consumeDataVo.setAllSeExam(amount.subtract(perExam));
        return consumeDataVo;
    }

    @Override
    public ConsumeDataVo mySummary(TPackConsumeRecords tPackConsumeRecords) {
        ConsumeDataVo consumeDataVo = new ConsumeDataVo();
        BigDecimal amount = tPackConsumeRecordsMapper.amountByAll(tPackConsumeRecords);
        consumeDataVo.setAllConsume(amount);
        BigDecimal perExam = tPackConsumeRecordsMapper.payByAll(tPackConsumeRecords);
        consumeDataVo.setAllPerExam(perExam);
        consumeDataVo.setAllSeExam(amount.subtract(perExam));
        return consumeDataVo;
    }

    /**
     * 查询外包消耗记录
     * 
     * @param id 外包消耗记录主键
     * @return 外包消耗记录
     */
    @Override
    public TPackConsumeRecords selectTPackConsumeRecordsById(Long id)
    {
        return tPackConsumeRecordsMapper.selectTPackConsumeRecordsById(id);
    }

    /**
     * 查询外包消耗记录列表
     * 
     * @param tPackConsumeRecords 外包消耗记录
     * @return 外包消耗记录
     */
    @Override
    public List<TPackConsumeRecords> selectTPackConsumeRecordsList(TPackConsumeRecords tPackConsumeRecords)
    {
        return tPackConsumeRecordsMapper.selectTPackConsumeRecordsList(tPackConsumeRecords);
    }

    @Override
    public void setPrice(TPackConsumeRecords tPackConsumeRecords) {
        TPackConsumeRecords sub = new TPackConsumeRecords();
        sub.setTime(tPackConsumeRecords.getTime());
        sub.setProjectId(tPackConsumeRecords.getProjectId());
        sub.setUid(tPackConsumeRecords.getUid());
        BigDecimal price = tPackConsumeRecords.getPrice();
        List<TPackConsumeRecords> list = tPackConsumeRecordsMapper.selectTPackConsumeRecordsList(sub);
        if (!list.isEmpty()){
            for (TPackConsumeRecords packConsumeRecords : list) {
                packConsumeRecords.setPrice(price);
                BigDecimal zong = price.multiply(packConsumeRecords.getNum());
                BigDecimal our  = packConsumeRecords.getUnit().multiply(packConsumeRecords.getNum());
                BigDecimal camExam = zong.subtract(our).subtract(packConsumeRecords.getOtherAmount());
                packConsumeRecords.setCamExam(camExam);
                tPackConsumeRecordsMapper.updateTPackConsumeRecords(packConsumeRecords);
            }
        }
    }

    /**
     * 新增外包消耗记录
     * 
     * @param tPackConsumeRecords 外包消耗记录
     * @return 结果
     */
    @Override
    public int insertTPackConsumeRecords(TPackConsumeRecords tPackConsumeRecords)
    {
        tPackConsumeRecords.setCreateTime(DateUtils.getNowDate());
        return tPackConsumeRecordsMapper.insertTPackConsumeRecords(tPackConsumeRecords);
    }

    /**
     * 修改外包消耗记录
     * 
     * @param tPackConsumeRecords 外包消耗记录
     * @return 结果
     */
    @Override
    public int updateTPackConsumeRecords(TPackConsumeRecords tPackConsumeRecords)
    {
        return tPackConsumeRecordsMapper.updateTPackConsumeRecords(tPackConsumeRecords);
    }

    /**
     * 批量删除外包消耗记录
     * 
     * @param ids 需要删除的外包消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteTPackConsumeRecordsByIds(Long[] ids)
    {
        return tPackConsumeRecordsMapper.deleteTPackConsumeRecordsByIds(ids);
    }

    /**
     * 删除外包消耗记录信息
     * 
     * @param id 外包消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteTPackConsumeRecordsById(Long id)
    {
        return tPackConsumeRecordsMapper.deleteTPackConsumeRecordsById(id);
    }
}
